package com.tianqu.tms.core.security;

import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.entity.User;

/**
 * @author liugang
 */
public class AccessControllerImpl extends AccessController {
    private Logger logger = Logger.getLogger(this.getClass().getName());
    private final String FUNCID_ROOT = "T000";

    public boolean checkPermission(TmsUserProfile userProfile, String funcId)
            throws TmsException {
        int userClass = 0;
        int userId = 0;
        List functIdList = userProfile.getUserClassList();

        if (userProfile != null) {
            userId = userProfile.getUniqueId();
            userClass = userProfile.getUserClass();
        }
        StringBuffer msg = new StringBuffer("权限check:");
        msg.append("用户=").append(String.valueOf(userId)).append(",UserClass=")
                .append(userClass).append(",FunctionID=").append(funcId)
                .append(",权限=");

        boolean result = false;
        for (int i = 0; i < functIdList.size(); i++) {

            if (FUNCID_ROOT.equals(funcId)
                    || funcId.equals(functIdList.get(i))) {
                result = true;
                break;
            }
        }
        if (result) {
            msg.append("有权限");
        } else {
            msg.append("无权限");
        }
        logger.debug(msg.toString());
        return result;

		/*// 管理员
        if (User.SYSTEM_ADMINISTRATOR == userClass) {
			boolean ADMINISTRATOR = false;
			for (int i = 0; i < functIdList.size(); i++) {

				if (FUNCID_ROOT.equals(funcId)
					|| funcId.equals(functIdList.get(i))) {
					ADMINISTRATOR = true;
					break;
				}
			}
			if(ADMINISTRATOR){
				msg.append("有权限");
			}else {
				msg.append("无权限");
			}
			logger.debug(msg.toString());
			return ADMINISTRATOR;
		}
		// 操作员
		if (User.SYSTEM_OPERATER == userClass) {
			boolean OPERATER = false;
			for (int i = 0; i < functIdList.size(); i++) {

				if (FUNCID_ROOT.equals(funcId)|| funcId.equals(functIdList.get(i))) {
					OPERATER = true;
					break;
				}
			}
			if(OPERATER){
				msg.append("有权限");
			}else {
				msg.append("无权限");
			}
			logger.debug(msg.toString());
			return OPERATER;
		}
		
		// 会员
		if (User.SYSTEM_MEMBER == userClass) {
			boolean MEMBER = false;
			for (int i = 0; i < functIdList.size(); i++) {

				if (FUNCID_ROOT.equals(funcId)
						|| funcId.equals(functIdList.get(i))) {
					MEMBER = true;
					break;
				}
			}
			if(MEMBER){
				msg.append("有权限");
			}else {
				msg.append("无权限");
			}
			logger.debug(msg.toString());
			return MEMBER;
		}
		logger.debug(msg.toString());
		return true;*/
    }
}
